package itheima04;

import domain.Person;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author xxx
 * @date 2020/12/28 16:19
 * @description
 */
public class TestDemo {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        //1.获取Class类型的对象
        Person person=new Person();
        Class c=person.getClass();

        Class c1=Person.class;

        Class<?> c2=Class.forName("domain.Person");
        //获取所有构造函数
        Constructor[] constructor=c.getConstructors();
        for (Constructor constructor1 : constructor) {
            System.out.println(constructor1);
        }
//        public domain.Person(java.lang.String)
//        public domain.Person(int)
//        public domain.Person()
//        public domain.Person(java.lang.String,int)

        System.out.println("---------------");
         //查询所有public方法
        Method[] method=c.getMethods();

        for (Method method1 : method) {
            System.out.println(method1);
        }
        System.out.println("------------");
        //获取类的对象
        Object o=c.newInstance();
        System.out.println("A:"+o);
        //相当于Person  person1=new Person();
        // System.out.println(person1);

        //根据有参构造和获取的方法，放入对应的字符串或整数，输出展示

        Method m=  c.getMethod("setName",String.class);
        System.out.println(m);
        Object object= m.invoke(o,"hhhhh");
        System.out.println(object);
        System.out.println(o);

        Method n=c.getMethod("getAge");
        System.out.println(n);

        Object o1=n.invoke(o);
        System.out.println(o1);

        Method method1=c.getMethod("setAge",int.class);
        System.out.println(method1);

        Object o2= method1.invoke(o,18);
        System.out.println(o2);

        System.out.println(o);





    }
}
